import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Hfq from '../components/orders/Hfq.vue'
import Hm from '../components/orders/Hm.vue'
import Pppoe from '../components/orders/Pppoe.vue'
import Users from '../components/user/Users.vue'
import Goods from '../components/goods/Goods.vue'
import Goodapplie from '../components/goods/Goodapplie.vue'
import Myapplie from '../components/goods/Myapplie.vue'
import Spppoe from '../components/search/Spppoe.vue'
import Usersreports from '../components/reports/Usersreports.vue'
import Hfqreports from '../components/reports/Hfqreports.vue'



// 手机端
import Mlogin from '../components/mobile/login/Login.vue'
import Mwelcome from '../components/mobile/welcome.vue'
import Morders from '../components/mobile/orders.vue'
import Muser from '../components/mobile/user.vue'
import Mhome from '../components/mobile/Home.vue'

import Mhfq from '../components/mobile/orders/Hfq.vue'
import HSubmit from '../components/mobile/orders/HSubmit.vue'
import HZSubmit from '../components/mobile/orders/HZSubmit.vue'
import Mhm from '../components/mobile/orders/Hm.vue'
import HmSubmit from '../components/mobile/orders/HmSubmit.vue'
import HmZSubmit from '../components/mobile/orders/HmZSubmit.vue'
import Mpppoe from '../components/mobile/orders/Pppoe.vue'
import PppoeSubmit from '../components/mobile/orders/PppoeSubmit.vue'
import PppoeZSubmit from '../components/mobile/orders/PppoeZSubmit.vue'
import Mgoods from '../components/mobile/goods/Goods.vue'
import MMyapplie from '../components/mobile/goods/Myapplie.vue'
import MGoodapplie from '../components/mobile/goods/Goodapplie.vue'
import MSpppoe from '../components/mobile/search/Spppoe.vue'


Vue.use(VueRouter)

const routes = [
  // 电脑端
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/hfq', component: Hfq },
      { path: '/hm', component: Hm },
      { path: '/pppoe', component: Pppoe },
      { path: '/sellbuy', component: () => import('../components/orders/Sellbuy.vue') },
      { path: '/oldWN', component: () => import('../components/orders/oldWN.vue') },
      { path: '/users', component: Users },
      { path: '/usersreports', component: Usersreports },
      { path: '/hfqreports', component: Hfqreports },
      { path: '/goods', component: Goods },
      { path: '/hqgoods', component: () => import('../components/goods/HQGoods.vue') },
      { path: '/myapplie', component: Myapplie },
      { path: '/goodapplie', component: Goodapplie },
      { path: '/spppoe', component: Spppoe },
    ]
  },

  // 手机端
  { path: '/mlogin', component: Mlogin },
  {
    path: '/mhome',
    component: Mhome,
    redirect: '/mwelcome',
    children: [
      { path: '/mwelcome', component: Mwelcome },
      { path: '/morders', component: Morders },
      { path: '/muser', component: Muser },
      { path: '/mhfq', component: Mhfq },
      { path: '/hSubmit', name: 'hSubmit', component: HSubmit },
      { path: '/hZSubmit', component: HZSubmit },
      { path: '/mhm', component: Mhm },
      { path: '/hmSubmit', name: 'hmSubmit', component: HmSubmit },
      { path: '/hmZSubmit', component: HmZSubmit },
      { path: '/mpppoe', component: Mpppoe },
      { path: '/pppoeSubmit', name: 'pppoeSubmit', component: PppoeSubmit },
      { path: '/pppoeZSubmit', component: PppoeZSubmit },
      { path: '/msellbuys', component: () => import('../components/mobile/orders/sellbuy.vue') },
      { path: '/sellZSumit', component: () => import('../components/mobile/orders/sellZSumit.vue') },
      { path: '/sellSubmit', name: "sellSubmit", component: () => import('../components/mobile/orders/sellSubmit.vue') },
      { path: '/moldWN', component: () => import('../components/mobile/orders/oldWN.vue') },
      { path: '/oldWNZSubmit', component: () => import('../components/mobile/orders/oldWNZSubmit.vue') },
      { path: '/oldWNSubmit', name: "oldWNSubmit", component: () => import('../components/mobile/orders/oldWNSubmit.vue') },



      { path: '/mgoods', component: Mgoods },
      { path: '/mHQGoods', component: () => import('../components/mobile/goods/MHQGoods.vue') },
      { path: '/mmyapplie', component: MMyapplie },
      { path: '/mgoodapplie', component: MGoodapplie },
      { path: '/mspppoe', component: MSpppoe },
    ]
  }
]

const router = new VueRouter({
  routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to and from are Route Object,next() must be called to resolve the hook}
  // to将要访问的路径
  // from 代表从哪个路径跳转而来
  // next是一个函数，表示放行
  // next()放行 next('/login')强制跳转
  if (to.path === '/login' || to.path === '/mlogin') return next();
  // 获取token
  const tokenStr = window.localStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})


export default router
